package com.imooc.controller;

import com.imooc.enums.ErrorMsg;
import com.imooc.pojo.bo.ShopcartBO;
import com.imooc.pojo.vo.ShopcartVO;
import com.imooc.service.ItemService;
import com.imooc.utils.CommonJsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: 逸尘风清
 * @Date: 2021/5/9
 * @Time: 16:59
 * 描述：
 */

@Api(value = "购物车接口" , tags = {"购物车相关接口"})
@RestController
@RequestMapping("cart")
public class ShopCartController {
    @Resource
    private ItemService itemService;

    @ApiOperation(value = "添加商品到购物车" , notes = "添加商品到购物车")
    @PostMapping("/add")
    public CommonJsonResult add(
                @RequestParam String userId,
                @RequestBody ShopcartBO shopCartBo,
                HttpServletRequest request,
                HttpServletResponse response
            )
    {
        if(StringUtils.isBlank(userId)){
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PARAMS);
        }
        // TODO
        return CommonJsonResult.ok();
    }

    @ApiOperation(value = "根据商品规格id获取商品" , notes = "根据商品规格id获取商品")
    @GetMapping("/refresh")
    public CommonJsonResult refresh(
            @ApiParam(value = "商品规格id", required = true)
            @RequestParam String itemSpecIds
            )
    {
        if(StringUtils.isBlank(itemSpecIds)){
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PARAMS);
        }
        List<ShopcartVO> list = itemService.queryItemsBySpecIds(itemSpecIds);
        return CommonJsonResult.ok(list);
    }

    @ApiOperation(value = "从购物车中删除商品" , notes = "从购物车中删除商品")
    @PostMapping("/del")
    public CommonJsonResult del(
            @RequestParam String userId,
            @RequestParam String itemSpecId,
            HttpServletRequest request,
            HttpServletResponse response
    )
    {
        if(StringUtils.isBlank(userId) || StringUtils.isBlank(itemSpecId)){
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PARAMS);
        }
        // TODO
        return CommonJsonResult.ok();
    }
}
